package com.feinno.urcs.group.dao;

import com.feinno.database.DataRow;
import com.feinno.database.DataTable;
import com.feinno.database.Database;
import com.feinno.urcs.group.GroupConfiguration;
import com.feinno.urcs.group.api.UserGroupListDao;
import com.feinno.urcs.group.entity.GroupIndex;
import com.feinno.urcs.types.MobileNo;
import org.helium.framework.annotations.FieldSetter;
import org.helium.framework.annotations.ServiceImplementation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by honghao on 5/4/15.
 * 因为使用GRPDB 所以废弃
 */
@Deprecated
@ServiceImplementation
public class UserGroupListDaoImpl implements UserGroupListDao {
	private static final Logger LOGGER = LoggerFactory.getLogger(UserGroupListDaoImpl.class);

	@FieldSetter("${GRPDB}")
	private Database grpDb;


	@Override
	public boolean canCreateGroup(MobileNo mobileNo) throws SQLException {
		int count = getCreateGroupCount(mobileNo);
		return count < GroupConfiguration.INSTANCE.getCreateGroupMaxCount();
	}

	@Override
	public int getGroupCount(MobileNo mobileNo) throws SQLException {
		String sql = "select count(GroupId) from GRP_UserGroups where MobileNo = ? ";
		DataTable dt = grpDb.executeTable(sql, mobileNo.toString(), 1);
		int count = 0;
		if (dt.getRowCount() > 0) {
			count = dt.getRow(0).getInt(1);
		}
		return count;
	}

	@Override
	public int getCreateGroupCount(MobileNo mobileNo) throws SQLException {
		String sql = "select count(GroupId) from GRP_UserGroups where MobileNo = ? and Identity = ?";
		DataTable dt = grpDb.executeTable(sql, mobileNo.toString(), 1);
		int count = 0;
		if (dt.getRowCount() > 0) {
			count = dt.getRow(0).getInt(1);
		}
		return count;
	}


	@Override
	public List<GroupIndex> getUserGroupList(MobileNo mobileNo) throws SQLException {
		String sql = "select GroupId,GroupLogicalPool from GRP_UserGroups where MobileNo = ?";
		DataTable dt = grpDb.executeTable(sql, mobileNo.toString());
		List<GroupIndex> groupList = new ArrayList<GroupIndex>();
		for (DataRow dr : dt.getRows()) {
			GroupIndex index = new GroupIndex();
			index.setGroupId(dr.getInt("GroupId"));
			index.setLogicalPool(dr.getInt("GroupLogicalPool"));
			groupList.add(index);
		}
		return groupList;
	}


	@Override
	public void updateUserGroupIdentity(MobileNo mobileNo, GroupIndex index, int identity) throws SQLException {
		String sql = "update   GRP_UserGroups set Identity =? where GroupId =? and MobileNo=?";
		grpDb.executeNonQuery(sql, identity, index.getGroupId(), mobileNo.toString());

	}

	@Override
	public void updateUserGroupIdentityWithUserId(int userid, GroupIndex index, int identity) throws Exception {
		throw new RuntimeException(" not IMPL");
	}

	@Override
	public void insertUserGroupList(MobileNo mobileNo, GroupIndex index, int identity) throws Exception {
		//TODO 需要查询UserID
		int userId = new Random().nextInt(100000000);
		String sql = "insert into GRP_UserGroups(GroupId,GroupLogicalPool,MobileNo,UserId,GroupType,Identity) values(?,?,?,?,?,?)";

		String getMemberSql = "select * from GRP_UserGroups where GroupId = ? and MobileNo =  ?";
		DataTable dataTable = grpDb.executeTable(getMemberSql, index.getGroupId(), mobileNo.toString());
		if (dataTable.getRowCount() != 0) {
			// 处理重复写入数据的逻辑
			LOGGER.info(String.format("insertUserGroupList Member exist, groupId=%s member=%s ", index.getGroupId(), mobileNo));
			return;
		}
		grpDb.executeNonQuery(sql, index.getGroupId(), index.getLogicalPool(), mobileNo.toString(), userId, 1, identity);
	}


	@Override
	public void deleteUserGroupList(MobileNo mobileNo, GroupIndex index) throws SQLException {
		String sql = "delete from GRP_UserGroups where MobileNo = ? and GroupId = ?";
		grpDb.executeNonQuery(sql, mobileNo.toString(), index.getGroupId());
	}

	@Override
	public void dismissGroup(GroupIndex index) throws SQLException {
		String sql = "delete from GRP_UserGroups where  GroupId = ?";
		grpDb.executeNonQuery(sql, index.getGroupId());
	}
}
